package com.tan.kotlintest.fragment

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import com.tan.kotlintest.R
import com.tan.kotlintest.activity.TestPhotoMovieActivity
import com.tan.kotlintest.base.BaseFragment
import com.tan.kotlintest.util.LogUtil
import kotlinx.android.synthetic.main.fragment_home_layout.*

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述:  首页Fragment
 * 作者：txj
 * 创建时间：2018/6/14
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class HomeFragment : BaseFragment(), View.OnClickListener {

    private val GIRL = 0//女神
    private val VIDEO = 1//视频
    private val CITY = 2//同城

    override fun initLayout(): Int {
        return R.layout.fragment_home_layout
    }

    override fun initView() {
        category_iv.setOnClickListener(this)
        girl_ll.setOnClickListener(this)
        video_ll.setOnClickListener(this)
        city_ll.setOnClickListener(this)
        category_iv.setOnClickListener(this)
        initViewPager()
    }

    override fun onFirstVisible() {
        LogUtil.i("HomeFragment第一次可见")
    }

    /**
     * 初始化viewPager
     */
    private fun initViewPager() {
        val girlFragment = GirlFragment()
        val mFragmentList = ArrayList<Fragment>()
        mFragmentList.add(girlFragment)
        content_vp.adapter = object : FragmentStatePagerAdapter(childFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return mFragmentList[position]
            }

            override fun getCount(): Int {
                return mFragmentList.size
            }
        }
        content_vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                switchTab(position, true)
            }
        })
        content_vp.offscreenPageLimit = 1
        switchTab(GIRL, false)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.category_iv -> {//分类
                val intent = Intent(context, TestPhotoMovieActivity::class.java)
                startActivity(intent)
            }
            R.id.girl_ll -> {//女神
                switchTab(GIRL, false)
            }
            R.id.video_ll -> {//视频
                switchTab(VIDEO, false)
            }
            R.id.city_ll -> {//同城
                switchTab(CITY, false)
            }
        }
    }

    /**
     * 切换
     */
    private fun switchTab(position: Int, fromViewPager: Boolean) {
        when (position) {
            GIRL -> {//女神
                if (girl_pink_tv.isSelected || girl_v.visibility == View.VISIBLE) {
                    return
                }

                girl_pink_tv.isSelected = true
                girl_pink_tv.visibility = View.VISIBLE
                girl_v.visibility = View.VISIBLE
                girl_tv.visibility = View.GONE

                video_pink_tv.isSelected = false
                video_pink_tv.visibility = View.GONE
                video_v.visibility = View.INVISIBLE
                video_tv.visibility = View.VISIBLE

                city_pink_tv.isSelected = false
                city_pink_tv.visibility = View.GONE
                city_v.visibility = View.INVISIBLE
                city_tv.visibility = View.VISIBLE

            }
            VIDEO -> {//女神
                if (video_pink_tv.isSelected || video_v.visibility == View.VISIBLE) {
                    return
                }

                video_pink_tv.isSelected = true
                video_pink_tv.visibility = View.VISIBLE
                video_v.visibility = View.VISIBLE
                video_tv.visibility = View.GONE

                girl_pink_tv.isSelected = false
                girl_pink_tv.visibility = View.GONE
                girl_v.visibility = View.INVISIBLE
                girl_tv.visibility = View.VISIBLE

                city_pink_tv.isSelected = false
                city_pink_tv.visibility = View.GONE
                city_v.visibility = View.INVISIBLE
                city_tv.visibility = View.VISIBLE

            }
            CITY -> {//女神
                if (city_pink_tv.isSelected || city_v.visibility == View.VISIBLE) {
                    return
                }

                city_pink_tv.isSelected = true
                city_pink_tv.visibility = View.VISIBLE
                city_v.visibility = View.VISIBLE
                city_tv.visibility = View.GONE

                girl_pink_tv.isSelected = false
                girl_pink_tv.visibility = View.GONE
                girl_v.visibility = View.INVISIBLE
                girl_tv.visibility = View.VISIBLE

                video_pink_tv.isSelected = false
                video_pink_tv.visibility = View.GONE
                video_v.visibility = View.INVISIBLE
                video_tv.visibility = View.VISIBLE

            }
        }
    }

}